using System;
using System.Linq;
using Coderoom.ScrumPlanner.Data.Repositories;
using Coderoom.ScrumPlanner.Domain;

namespace Coderoom.ScrumPlanner.Tests.FakeRepositories
{
	public sealed class FakeTaskRepository : TaskRepository
	{
		private TaskCollection _data;

		public FakeTaskRepository()
		{
			SetupFakeData();
		}

		public override TaskCollection GetTasks(Guid userStoryId)
		{
			var returnData = _data.Where(task => task.UserStoryId == userStoryId);

			var themes = new TaskCollection();
			foreach (var theme in returnData)
				themes.Add(theme);

			return themes;
		}

		public override Task GetTask(Guid taskId)
		{
			return _data.Where(task => task.Id == taskId).SingleOrDefault();
		}

		public override void CreateTask(Task task)
		{
			task.Id = Guid.NewGuid();

			_data.Add(task);
		}

		public override bool DeleteTask(Guid taskId)
		{
			Task task;

			if ((task = GetTask(taskId)) != null)
				return _data.Remove(task);

			return false;
		}

		private void SetupFakeData()
		{
			_data = new TaskCollection();

			_data.Add(new Task {Id = new Guid("FDB854A9-9D52-414C-83DA-469DEAD2A0D8"), UserStoryId = new Guid("A5960D03-0648-4D17-BF55-8C0E86DDEC32"), Title = "Task A", EstimatedComplexity = 1});
			_data.Add(new Task {Id = new Guid("B84EF056-321E-4ACE-855F-3DB1EC5C03F4"), UserStoryId = new Guid("A5960D03-0648-4D17-BF55-8C0E86DDEC32"), Title = "Task B", EstimatedComplexity = 2});
			_data.Add(new Task {Id = new Guid("1DCE7A17-D9C4-4759-B1FA-A4A661E1DCC0"), UserStoryId = new Guid("5DB514B6-BFDC-44BC-AD75-7AF8D3BF0D71"), Title = "Task X", EstimatedComplexity = 13});

			foreach (var task in _data)
				task.Slug = Entity.CreateSlug(task.Title);
		}
	}
}